import axios from 'axios';

const API_URL = 'https://api.example.com/user';

export default {
  async login(username, password) {
    try {
      const response = await axios.post(`${API_URL}/login`, { username, password });
      localStorage.setItem('user', JSON.stringify(response.data));
      return response.data;
    } catch (error) {
      throw new Error('登录失败');
    }
  },

  logout() {
    localStorage.removeItem('user');
  },

  getCurrentUser() {
    return JSON.parse(localStorage.getItem('user'));
  },

  isAdmin() {
    const user = this.getCurrentUser();
    return user && user.role === 'admin';
  }
};